import { ADD_TODO, DEL_TODO, TOGGLE_ITEM } from '../actions/actionType'

const todos = (state = [], action) => {
  switch (action.type) {
    case ADD_TODO:   // 处理actions中添加过程
      return [
        ...state,
        { //新加的任务项
          id: action.id,
          text: action.text,
          ischeck: false
        }
      ]
      break;
    case DEL_TODO: //删除列表项的actions的具体处理
      state.forEach((item, index) => {
        if (action.id == item.id) {
          state.splice(index, 1)
        }
      })
      return [...state]
      break;
    case TOGGLE_ITEM: //处理切换完成未完成的reducer
      state.forEach((item, index) => {
        if (action.id == item.id) {
          state[index].ischeck = !state[index].ischeck;
        }
      })
      return [...state]
      break;
    default:
      return state
  }
}
export default todos